home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / bplus20.zip / REINDEX.C < prev    next >
Text File  |  1987-11-12  |  4KB  |  100 lines

  1. /********************************************************************/
  2. /*                            REINDEX.C                             */
  3. /*                                                                  */
  4. /* This is a sample program which demonstrates how to reindex a     */
  5. /* data file.  The data file which is used is the one created by    */
  6. /* NAMES.C which creates an online address book using the B-PLUS    */
  7. /* file indexing toolkit.                                           */
  8. /*                                                                  */
  9. /********************************************************************/
  10.  
  11.  
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include "bplus.h"
  15.  
  16.  
  17. typedef struct              /* Here is the address record definition */
  18.   {
  19.      char lastname[16];     /* last name           */
  20.      char firstname[16];    /* first name          */
  21.      char address1[31];     /* first address line  */
  22.      char address2[31];     /* second address line */
  23.      char city[21];         /* the city            */
  24.      char state[3];         /* the state           */
  25.      char zipcode[6];       /* postal zip code     */
  26.      char telephone[14];    /* telephone number    */
  27.   }  ADDRESS;
  28.  
  29.  
  30. IX_DESC  nameindex;             /* index file variable  */
  31. FILE     *namefile;             /* data file pointer    */
  32. ADDRESS  person;                /* data record variable */
  33.  
  34.  
  35. void openfiles(void);
  36. void closefiles(void);
  37. long makeindex(void);
  38.  
  39.  
  40. void openfiles()
  41.   /* The file NAMES.DAT already exists.  It is  opened and a new  */
  42.   /* index file is created.                                       */
  43.   {
  44.     if ((namefile = fopen("names.dat","r")) != NULL)
  45.       make_index("names.idx", &nameindex, 1);   /* create index file */
  46.     else
  47.     {
  48.       printf("\nUnable to open NAMES.DAT\n");
  49.       exit(1);
  50.     }
  51.   } /* openfiles */
  52.  
  53.  
  54. void closefiles()
  55.   /* close all files and exit */
  56.   {
  57.     fclose(namefile);
  58.     close_index(&nameindex);
  59.   } /* closefiles */
  60.  
  61.  
  62. long makeindex()
  63.   /* read address records and add key to index file */
  64.   {
  65.     ENTRY ee;
  66.     long num, position, size;
  67.     int  ret;
  68.     size = sizeof(person);                    /* data record size */
  69.     ret = fread(&person,size,1,namefile);     /* ret is number of data  */
  70.                                               /* records that were read */
  71.     num = 0L;                                 /* num = data items read */
  72.     position = 0L;                            /* position in datafile */
  73.     while (ret == 1)
  74.       {
  75.         strcpy(ee.key, person.lastname);      /* key is last name followed */
  76.         strcat(ee.key, person.firstname);     /* first name.  Capitalize   */
  77.         strupr(ee.key);                  /*    and copy to ee.key.    */
  78.         ee.recptr = position;                 /* position in datafile  */
  79.         if (add_key(&ee, &nameindex) != IX_OK)     /* add key to index */
  80.            printf("Error while adding key to index file");
  81.         position += size;                     /* new position in datafile */
  82.         num++;                                /* increment data items read */
  83.         ret = fread(&person,sizeof(person),1,namefile);
  84.       }
  85.       return ( num );
  86.   } /* makeindex */
  87.  
  88.  
  89. main()
  90.   /* Here is the main program */
  91.   {
  92.     long num;
  93.     openfiles();
  94.     printf("\n\n     MAKING THE NEW INDEX FILE\n\n");
  95.     num = makeindex();
  96.     closefiles();
  97.     printf("     REINDEXING IS COMPLETE - %ld ITEMS WERE INDEXED\n", num);
  98.   } /* main */
  99.  
  100.